﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Net;
using System.Threading;

namespace MyData.Activities
{
  [Designer(typeof(MyData.Activities.Designers.LoginFormActivityDesigner))]
  public sealed class LoginFormActivity : CodeActivity
  {
    //How to auto-set from WF argument?
    public InArgument<IntPtr> Handle {get; set;}

    public OutArgument<ICredentials> NetworkCredential { get; set; }

    private ICredentials _networkCredential;

    protected override void Execute(CodeActivityContext context)
    {    
      IntPtr handle = Handle.Get(context);

      LoginForm form = new LoginForm();
      form.DataAvailable += new LoginForm.ActivityFormEventHandler(form_DataAvailable);    
      form.ShowDialog(new MyWin32Window(handle));

      NetworkCredential.Set(context, _networkCredential);

    }

    //Get the users credentials
    void form_DataAvailable(object sender, ActivityFormEventArgs e)
    {
      _networkCredential = e.NetworkCredential;
    }
  }

  public class MyWin32Window : IWin32Window
  {
    private IntPtr _hwnd;

    public MyWin32Window(IntPtr handle)
    {
      _hwnd = handle;
    }

    public IntPtr Handle
    {
      get
      {
        return _hwnd;
      }
    }
  }
}
